home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / ImtImagePlugin.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  1.7 KB  |  89 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: ImtImagePlugin.py,v 1.1.1.1 1998/08/18 13:07:51 sjoerd Exp $
  4. #
  5. # IM Tools support for PIL
  6. #
  7. # history:
  8. #    96-05-27 fl    Created (read 8-bit images only)
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1996.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15.  
  16.  
  17. __version__ = "0.1"
  18.  
  19. import regex, string
  20.  
  21. import Image, ImageFile
  22.  
  23. #
  24. # --------------------------------------------------------------------
  25.  
  26. field = regex.compile("\([a-z]*\) \([^ \r\n]*\)")
  27.  
  28. class ImtImageFile(ImageFile.ImageFile):
  29.  
  30.     format = "IMT"
  31.     format_description = "IM Tools"
  32.  
  33.     def _open(self):
  34.  
  35.     # Quick rejection: if there's not a LF among the first
  36.     # 100 bytes, this is (probably) not a text header.
  37.  
  38.     if not "\n" in self.fp.read(100):
  39.         raise SyntaxError, "not an IM file"
  40.     self.fp.seek(0)
  41.  
  42.     xsize = ysize = 0
  43.  
  44.     while 1:
  45.  
  46.         s = self.fp.read(1)
  47.         if not s:
  48.         break
  49.  
  50.         if s == chr(12):
  51.  
  52.         # image data begins
  53.             self.tile = [("raw", (0,0)+self.size,
  54.                  self.fp.tell(),
  55.                      (self.mode, 0, 1))]
  56.  
  57.         break
  58.  
  59.         else:
  60.  
  61.         # read key/value pair
  62.         # FIXME: dangerous, may read whole file
  63.         s = s + self.fp.readline()
  64.         if len(s) == 1 or len(s) > 100:
  65.             break
  66.         if s[0] == "*":
  67.             continue # comment
  68.  
  69.         if field.match(s) < 0:
  70.             break
  71.         k, v = field.group(1,2)
  72.         if k == "width":
  73.             xsize = string.atoi(v)
  74.             self.size = xsize, ysize
  75.         elif k == "height":
  76.             ysize = string.atoi(v)
  77.             self.size = xsize, ysize
  78.         elif k == "pixel" and v == "n8":
  79.             self.mode = "L"
  80.             
  81.  
  82. #
  83. # --------------------------------------------------------------------
  84.  
  85. Image.register_open("IMT", ImtImageFile)
  86.  
  87. #
  88. # no extension registered (".im" is simply too common)
  89.